package com.sdp.right.control;


import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.right.db.sys_role_person.service.ISysRolePersonService;
import com.sdp.right.db.sys_role_person.service.impl.SysRolePersonServiceImpl;
import com.sdp.right.entity.SysRolePerson;
import com.sdp.right.view.SysRolePersonView;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsp
 * @since 2023-10-12
 */
@RestController
@RequestMapping("/SysRolePersonController")
public class SysRolePersonController {

	@Autowired
	private Query query;
	
	@Autowired
	private ISysRolePersonService iSysRolePersonService;
	
	@Autowired
	private SysRolePersonServiceImpl sysRolePersonServiceImpl;
	
	
	@PostMapping(value = "/page")
	public Result<?> page(@RequestBody DataCenter dataCenter) {
		QueryWrapper<SysRolePersonView> queryWrapper = new QueryWrapper<>();
		return Result.ok(query.viewPage(sysRolePersonServiceImpl, SysRolePersonView.class, dataCenter, queryWrapper));
	}
	
	@PostMapping(value = "/save")
	public Result<?> list(@RequestBody JSONObject obj) {
		if(obj==null){
			return Result.error("人员信息不存在");
		}
		List<SysRolePerson> sysRolePersons = JSONArray.parseArray(JSON.toJSONString(obj.get("empList")), SysRolePerson.class);
		String roleNo = obj.getString("roleNo");
		if(!StringUtils.isEmpty(roleNo) && sysRolePersons!=null && sysRolePersons.size()>0) {
			List<SysRolePerson> personList = iSysRolePersonService.querySysRolePersonByRoleNo(roleNo);
			if(personList==null || personList.size()==0) {
				iSysRolePersonService.saveBatch(sysRolePersons);
			}else {
				personList.addAll(sysRolePersons);
				iSysRolePersonService.saveOrUpdateBatch(personList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(()->new TreeSet<>(Comparator.comparing(SysRolePerson :: getAccountNo))), ArrayList::new)));
			}

		}
		return Result.ok();
	}
	
	@GetMapping(value = "/del")
	@Transactional(rollbackFor=Exception.class)
	@DS(value="right")
	public Result<?> del(@RequestParam(name="keyId",required=true) String keyId) {
		if(StringUtils.isEmpty(keyId) ) {
			return Result.error("需要删除的记录的主键不存在");
		}
		iSysRolePersonService.removeById(keyId);

		return Result.ok();
	}
}


